<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品id" prop="productId">
        <el-input v-model="queryParams.productId" placeholder="请输入商品id" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="活动标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入活动标题" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="活动属性" prop="attr">
        <el-input v-model="queryParams.attr" placeholder="请输入活动属性" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="参团人数" prop="people">
        <el-input v-model="queryParams.people" placeholder="请输入参团人数" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="简介" prop="info">
        <el-input v-model="queryParams.info" placeholder="请输入简介" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入价格" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商品原价" prop="productPrice">
        <el-input v-model="queryParams.productPrice" placeholder="请输入商品原价" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="queryParams.sort" placeholder="请输入排序" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="销量" prop="sales">
        <el-input v-model="queryParams.sales" placeholder="请输入销量" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="库存" prop="stock">
        <el-input v-model="queryParams.stock" placeholder="请输入库存" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="推荐" prop="isHost">
        <el-input v-model="queryParams.isHost" placeholder="请输入推荐" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品状态" prop="isShow">
        <el-input v-model="queryParams.isShow" placeholder="请输入产品状态" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="删除标识" prop="isDel">
        <el-input v-model="queryParams.isDel" placeholder="请输入删除标识" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="删除标识" prop="combination">
        <el-input v-model="queryParams.combination" placeholder="请输入删除标识" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商户是否可用1可用0不可用" prop="merUse">
        <el-input v-model="queryParams.merUse" placeholder="请输入商户是否可用1可用0不可用" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="拼团开始时间" prop="startTime">
        <el-date-picker clearable size="mini" v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
          placeholder="选择拼团开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="拼团结束时间" prop="stopTime">
        <el-date-picker clearable size="mini" v-model="queryParams.stopTime" type="date" value-format="yyyy-MM-dd"
          placeholder="选择拼团结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="拼团订单有效时间" prop="effectiveTime">
        <el-input v-model="queryParams.effectiveTime" placeholder="请输入拼团订单有效时间" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="拼图产品成本" prop="cost">
        <el-input v-model="queryParams.cost" placeholder="请输入拼图产品成本" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="浏览量" prop="browse">
        <el-input v-model="queryParams.browse" placeholder="请输入浏览量" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="单位名" prop="unitName">
        <el-input v-model="queryParams.unitName" placeholder="请输入单位名" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="规格 0单 1多" prop="specType">
        <el-select v-model="queryParams.specType" placeholder="请选择规格 0单 1多" clearable size="mini">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="运费模板ID" prop="tempId">
        <el-input v-model="queryParams.tempId" placeholder="请输入运费模板ID" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:combination:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:combination:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:combination:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['system:combination:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="combinationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="运费模板ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="商品id" align="center" prop="productId" />
      <el-table-column label="推荐图" align="center" prop="image" />
      <el-table-column label="轮播图" align="center" prop="images" />
      <el-table-column label="活动标题" align="center" prop="title" />
      <el-table-column label="活动属性" align="center" prop="attr" />
      <el-table-column label="参团人数" align="center" prop="people" />
      <el-table-column label="简介" align="center" prop="info" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="商品原价" align="center" prop="productPrice" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="销量" align="center" prop="sales" />
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="推荐" align="center" prop="isHost" />
      <el-table-column label="产品状态" align="center" prop="isShow" />
      <el-table-column label="删除标识" align="center" prop="isDel" />
      <el-table-column label="删除标识" align="center" prop="combination" />
      <el-table-column label="商户是否可用1可用0不可用" align="center" prop="merUse" />
      <el-table-column label="拼团内容" align="center" prop="description" />
      <el-table-column label="拼团开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拼团结束时间" align="center" prop="stopTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="拼团订单有效时间" align="center" prop="effectiveTime" />
      <el-table-column label="拼图产品成本" align="center" prop="cost" />
      <el-table-column label="浏览量" align="center" prop="browse" />
      <el-table-column label="单位名" align="center" prop="unitName" />
      <el-table-column label="规格 0单 1多" align="center" prop="specType" />
      <el-table-column label="运费模板ID" align="center" prop="tempId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:combination:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:combination:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>

    <!-- 添加或修改拼团产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" size="mini"/>
        </el-form-item>
        <el-form-item label="推荐图">
          <imageUpload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="轮播图" prop="images">
          <el-input v-model="form.images" type="textarea" placeholder="请输入内容" size="mini"/>
        </el-form-item>
        <el-form-item label="活动标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入活动标题" size="mini"/>
        </el-form-item>
        <el-form-item label="活动属性" prop="attr">
          <el-input v-model="form.attr" placeholder="请输入活动属性" size="mini"/>
        </el-form-item>
        <el-form-item label="参团人数" prop="people">
          <el-input v-model="form.people" placeholder="请输入参团人数" size="mini"/>
        </el-form-item>
        <el-form-item label="简介" prop="info">
          <el-input v-model="form.info" placeholder="请输入简介" size="mini"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" size="mini"/>
        </el-form-item>
        <el-form-item label="商品原价" prop="productPrice">
          <el-input v-model="form.productPrice" placeholder="请输入商品原价" size="mini"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" size="mini"/>
        </el-form-item>
        <el-form-item label="销量" prop="sales">
          <el-input v-model="form.sales" placeholder="请输入销量" size="mini"/>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" size="mini"/>
        </el-form-item>
        <el-form-item label="添加时间" prop="createTime">
          <el-date-picker clearable size="mini"
            v-model="form.createTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择添加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime">
          <el-date-picker clearable size="mini"
            v-model="form.updateTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="推荐" prop="isHost">
          <el-input v-model="form.isHost" placeholder="请输入推荐" size="mini"/>
        </el-form-item>
        <el-form-item label="产品状态" prop="isShow">
          <el-input v-model="form.isShow" placeholder="请输入产品状态" size="mini"/>
        </el-form-item>
        <el-form-item label="删除标识" prop="isDel">
          <el-input v-model="form.isDel" placeholder="请输入删除标识" size="mini"/>
        </el-form-item>
        <el-form-item label="删除标识" prop="combination">
          <el-input v-model="form.combination" placeholder="请输入删除标识" size="mini"/>
        </el-form-item>
        <el-form-item label="商户是否可用1可用0不可用" prop="merUse">
          <el-input v-model="form.merUse" placeholder="请输入商户是否可用1可用0不可用" size="mini"/>
        </el-form-item>
        <el-form-item label="拼团内容" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" size="mini"/>
        </el-form-item>
        <el-form-item label="拼团开始时间" prop="startTime">
          <el-date-picker clearable size="mini"
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择拼团开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拼团结束时间" prop="stopTime">
          <el-date-picker clearable size="mini"
            v-model="form.stopTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择拼团结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拼团订单有效时间" prop="effectiveTime">
          <el-input v-model="form.effectiveTime" placeholder="请输入拼团订单有效时间" size="mini"/>
        </el-form-item>
        <el-form-item label="拼图产品成本" prop="cost">
          <el-input v-model="form.cost" placeholder="请输入拼图产品成本" size="mini"/>
        </el-form-item>
        <el-form-item label="浏览量" prop="browse">
          <el-input v-model="form.browse" placeholder="请输入浏览量" size="mini"/>
        </el-form-item>
        <el-form-item label="单位名" prop="unitName">
          <el-input v-model="form.unitName" placeholder="请输入单位名" size="mini"/>
        </el-form-item>
        <el-form-item label="规格 0单 1多" prop="specType">
          <el-select v-model="form.specType" placeholder="请选择规格 0单 1多" size="mini">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="运费模板ID" prop="tempId">
          <el-input v-model="form.tempId" placeholder="请输入运费模板ID" size="mini"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCombination, getCombination, delCombination, addCombination, updateCombination, exportCombination } from "@/api/module/product/combination";

export default {
  name: "Combination",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 拼团产品表格数据
      combinationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productId: undefined,
        image: undefined,
        images: undefined,
        title: undefined,
        attr: undefined,
        people: undefined,
        info: undefined,
        price: undefined,
        productPrice: undefined,
        sort: undefined,
        sales: undefined,
        stock: undefined,
        isHost: undefined,
        isShow: undefined,
        isDel: undefined,
        combination: undefined,
        merUse: undefined,
        description: undefined,
        startTime: undefined,
        stopTime: undefined,
        effectiveTime: undefined,
        cost: undefined,
        browse: undefined,
        unitName: undefined,
        specType: undefined,
        tempId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询拼团产品列表 */
    getList() {
      this.loading = true;
      listCombination(this.queryParams).then(response => {
        this.combinationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productId: undefined,
        image: undefined,
        images: undefined,
        title: undefined,
        attr: undefined,
        people: undefined,
        info: undefined,
        price: undefined,
        productPrice: undefined,
        sort: undefined,
        sales: undefined,
        stock: undefined,
        createTime: undefined,
        updateTime: undefined,
        isHost: undefined,
        isShow: undefined,
        isDel: undefined,
        combination: undefined,
        merUse: undefined,
        description: undefined,
        startTime: undefined,
        stopTime: undefined,
        effectiveTime: undefined,
        cost: undefined,
        browse: undefined,
        unitName: undefined,
        specType: undefined,
        tempId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加拼团产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCombination(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改拼团产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCombination(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCombination(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除拼团产品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delCombination(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有拼团产品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCombination(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
